package com.example.layoutandlistview;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class FileDownload {
	private String fileName;
	private String downloadLink;
	private String description; 
	public FileDownload(String fileName, String urlDownload, String des) {
		this.fileName = fileName;
		this.downloadLink = urlDownload;
		this.description = des;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setDownloadLink(String url) {
		this.downloadLink = url;
	}

	public String getFileName() {
		return this.fileName;
	}

	public String getDownloadLink() {

		return this.downloadLink;
	}
	
	public String getDescription(){
		return this.description;
		
	}

	public void setDescription(String des){
		this.description = des;
	}
	public static ArrayList<FileDownload> getFileList(String placeString) {

		Log.i("search", placeString);
		// string save http response
		String resString = "";
		BufferedReader in = null;
		ArrayList<FileDownload> fileList = new ArrayList<FileDownload>();
		try {
			HttpClient client = new DefaultHttpClient();
			String reqString = "http://nominatim.openstreetmap.org/search.php?q="
					+ "ha+noi" + "&format=xml";// true

			HttpGet method = new HttpGet(reqString);
			HttpResponse response = client.execute(method);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			Log.i("test", "run 1");
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			resString = sb.toString();
			Log.i("parse", resString + resString.length());
			Log.i("test", "run 2");

		} catch (Exception e) {
			// TODO: handle exception
			Log.i("test", "run 3");

		}

		int index = 0;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xmlPullParser = factory.newPullParser();

			// xmlPullParser.setInput(new StringReader("<a>value</>"));
			// for parse string xml structure
			xmlPullParser.setInput(new StringReader(resString));

			String fileName = "";
			String link = "";
			String des="descripton";
			int eventType;

			while ((eventType = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {

					if (xmlPullParser.getName().equals("place")) {
						// get attribute
						int attributeCount = xmlPullParser.getAttributeCount();
						for (int i = 0; i < attributeCount; i++) {
							if (xmlPullParser.getAttributeName(i).equals(
									"display_name")) {
								index++;
								link = xmlPullParser
										.getAttributeValue(i);
								Log.i("display_name", link);
							} else if (xmlPullParser.getAttributeName(i)
									.equals("type")) {
								index++;
								fileName = xmlPullParser.getAttributeValue(i);
								Log.i("type", fileName);
								if (index == 2) {
									fileList.add(new FileDownload(fileName,
											link, des));
									index = 0;
								}
							}
							// because icon is final attribute of place tag

						}// get attribute
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.i("tra lai", String.valueOf(fileList.size()));
		return fileList;
	}
}
